# import cu as cu
from otree.api import BasePlayer

from . import models
from ._builtin import Page, WaitPage


class Introduction(Page):
    def is_displayed(self):
        return self.round_number == 1
class Guess(Page):
    form_model = 'player'
    form_fields = ['guess']

    # 这是一个更灵活的去选择timeout_seconds，所以你可以让超时取决于玩家，player.session,等等。
    # # 在这里你可以定义需要在表单验证之后，玩家继续下一个页面之前执行的任意代码。
    # # 如果页面通过is_displayed被跳过，那么before_next_page也会被跳过
    # # 用于让机器人输入数据
    def get_timeout_seconds(self):
        if self.participant.vars.get('is_dropout'):
            return 1  # instant timeout, 1 second
        else:
            return 15

    def before_next_page(self):
        player = self.player
        timeout_happened = self.timeout_happened
        if timeout_happened:
            #这里是值 可以随机加入 模仿机器人
            player.guess = 66
            self.participant.vars['is_dropout'] = True

    # def app_after_this_page(self, upcoming_apps):
    #     timeout_happened = self.timeout_happened
    #     if timeout_happened:
    #         # 这里是值 可以随机加入 模仿机器人
    #         # 6个玩家 <7
    #         # self.participant.vars['is_dropout'] = True
    #         return upcoming_apps[0]


class ResultsWaitPage(WaitPage):
    after_all_players_arrive = 'set_payoffs'


class Results(Page):
    def vars_for_template(self):
        sorted_guesses = sorted(p.guess for p in self.group.get_players())

        return dict(sorted_guesses=sorted_guesses)


page_sequence = [Introduction, Guess, ResultsWaitPage, Results]
